package com.cpit.platform.common.dao;

import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;

/** 
 * 函数条件表达式 暂时不用，待处理
 */
public class ProjectionExpression implements Criterion {
	private String fieldName;       //属性名  
    private Object value;           //对应值 
    private Projection projection;  //函数条件类型
    private Operator operator;      //基础条件类型
    private MatchMode matchMode;    //like用匹配类型
    
    protected ProjectionExpression(String fieldName, Object value, Projection projection, Operator operator) {
        this.fieldName = fieldName;  
        this.value = value;  
        this.projection = projection;
        this.operator = operator;
    }  
    protected ProjectionExpression(String fieldName, Object value, Projection projection, Operator operator, MatchMode matchMode) {  
    	this.fieldName = fieldName;  
    	this.value = value;  
    	this.projection = projection;
    	this.operator = operator;
    	this.matchMode = matchMode;
    }  
  
	
	@SuppressWarnings({ "rawtypes", "unchecked" })  
    public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
            CriteriaBuilder builder) {  
        Path expression = Criterion.getPath(root,fieldName);
        if (expression == null){
            return null;
        }
        switch (projection) {  
        case LENGTH :
            return Criterion.toSimplePredicate(operator,builder, builder.length(expression), value, matchMode);
        case MAX :
        	return Criterion.toSimplePredicate(operator,builder, builder.max(expression), value, matchMode);
        case SUM :
        	return Criterion.toSimplePredicate(operator,builder, builder.sum(expression), value, matchMode);
        case MIN :
        	return Criterion.toSimplePredicate(operator,builder, builder.min(expression), value, matchMode);
        case AVG :
        	return Criterion.toSimplePredicate(operator,builder, builder.avg(expression), value, matchMode);
        case COUNT :
        	return Criterion.toSimplePredicate(operator,builder, builder.count(expression), value, matchMode);
        default:
            return null;  
        }  
    }  
	

	public String getFieldName() {  
        return fieldName;  
    }  
    public Object getValue() {  
        return value;  
    }  
	public Projection getProjection() {
		return projection;
	}
	public Operator getOperator() {
		return operator;
	}
	public MatchMode getMatchMode() {
		return matchMode;
	}
}
